package com.hmdp.config;

import com.hmdp.interceptor.LoginInterceptor;
import com.hmdp.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 配置类
 * 1、实现 WebMvcConfigurer类，注册拦截器
 * 2、所有拦截器会被注册成 InterceptorRegistration 对象，
 * （1）它有一个属性order默认是0，数值越小优先级越高，我们可以手动控制
 * （2）当然我们也可以按照顺序来注册拦截器，它们就会按顺序执行
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登录拦截器
        registry.addInterceptor(new LoginInterceptor()).
                excludePathPatterns(                //排除拦截路径（添加直接放行的路径）
                        "/shop/**",
                        "/voucher/**",
                        "/upload/**",
                        "/shop-type/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login"
                ).order(1);
        // 刷新token拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
        // registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate));   // 默认拦截所有请求
    }
}
